package com.job51.hro.datacheckservice.util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author pengfei.ma
 * @since 1.0.0
 */
public class DateUtil {
    /**
     * 生成对应pattern时间的字符串 ，可用于文件名命名等
     */
    public static String getString(String pattern) {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(date);
    }

    public static String getString(Date date, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(date);
    }

    /**
     * 获取精确到秒的时间戳
     *
     */
    public static long getSecondTimestamp(Date date) {
        if (null == date) {
            return 0;
        }
        String timestamp = String.valueOf(date.getTime() / 1000);
        return Long.parseLong(timestamp);
    }

    /** 根据时间字符和格式返回 时间戳
     *
     */
    public static long getSecondTimestamp(String dateString, String pattern) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = simpleDateFormat.parse(dateString);
        return date.getTime()/1000;
    }

}
